<?php
class roleController extends AController{	
	private $tableName='ROLE';

	public function indexAction(){
		$allgroup = $this->getAllGroupInfo();
		$this->groups = $allgroup;
		return $this->render();
	}
	public function addAction(){
		$id = $this->request->getGetParameter('id');
		if(isset($_POST['add_role'])){
			if(empty($id)){
				$upd['ROLE_NAME'] = $this->request->getPostParameter('name');
				$upd['DESCRIPTION'] = $this->request->getPostParameter('description');
				if(empty($upd['ROLE_NAME'])){
					$this->alert('请输入名称');exit;
				}
				if(JYDB::find($this->tableName,'role_name=\''.$upd['ROLE_NAME'].'\'')){
					$this->alert('用户组名称已存在');exit;
				}
				$unid = $this->request->getPostParameter('unid');
				$upd['MENUS'] = implode(',',$this->request->getPostParameter('menus'));
				$upd['ACTIONS'] = implode(',',$this->request->getPostParameter('actions'));
				$upd['UNIT_ID']= $unid?$unid:$this->unid;
				$upd['STATE'] = 1;
				$upd['UPD_TIME'] = date('Y-m-d H:i:s');
				$upd['UPD_USER'] = $this->username;
				if($this->createGroup($upd)){
					$this->alert('操作成功');exit;
				}
				$this->alert('系统繁忙，请重试');exit;
			}else{
				$upd['ROLE_NAME'] = $this->request->getPostParameter('name');
				$upd['DESCRIPTION'] = $this->request->getPostParameter('description');
				if(empty($upd['ROLE_NAME'])){
					$this->alert('请输入名称');exit;
				}
				$upd['MENUS'] = implode(',',$this->request->getPostParameter('menus'));
				$upd['ACTIONS'] = implode(',',$this->request->getPostParameter('actions'));
				$upd['UPD_TIME'] = date('Y-m-d H:i:s');
				$upd['UPD_USER'] = $this->username;
				if($this->updateGroup($id,$upd)){
					$this->alert('操作成功');exit;
				}
				$this->alert('系统繁忙，请重试');exit;
			}
		}
		if(!empty($id)){
			$roleinfo = $this->getRoleInfoById($id);
		}
		$haverole = $this->getRoleInfoById($_SESSION['user']['role_id']);
		$haveaction = explode(',',$haverole['ACTIONS']);
		$havemenu = explode(',',$haverole['MENUS']);
//		var_dump($havemenu);
		$allmenu = ATools::getConfig('menu');
		$allunit = $this->getAllUnitName();
		foreach ($allunit as $v){
			$allUnit[$v['UID']] = $v['UN'];
		}
		unset($allunit);
		$menus = array('menus1');
		$actions = array('1101');
		$allgroup = $this->getAllGroupInfo();
		$this->groups = $allgroup;
		$this->id = $id;
		$this->allunit = $allUnit;
		$this->roleinfo = $roleinfo;
		$this->allmenu = $allmenu;
		$this->haveaction = $haveaction;
		$this->havemenu = $havemenu;
		return $this->render();
	}
	public function delAction(){
		$id = $this->request->getGetParameter('id');
		if($this->delRoleById($id)){
			return json_encode(array('msg'=>'suc','code'=>1000));
		}
		return json_encode(array('msg'=>'fail','code'=>1001));
		
	}
	function getAllGroupInfo(){
		if($this->unid<0){
			$where = 'state>0';
		}else{
			$where = 'state>0 and unit_id='.$this->unid;
		}
		$sql = "select a.*,b.nums from ROLE a left join (select role_id,count(USER_ID) nums from sysuser where ".$where." group by role_id) b on a.role_id=b.role_id where 1=1 and ".$where;
		return JYDB::query($sql);
	}
	function getAllUnitName(){
		return JYDB::findAll('UNIT','unit_state>0','UNIT_ID "UID",UNIT_NAME UN','UNIT_SPELL');
	}
	function getRoleInfoById($id){
		return JYDB::find($this->tableName,'ROLE_ID='.$id);
	}
	function createGroup($upd){
		return JYDB::create($this->tableName,$upd,'ROLE_ID');
	}
	function updateGroup($id,$arr){
		return JYDB::update($this->tableName,'ROLE_ID='.$id,$arr);
	}
	function delRoleById($id){
		return JYDB::update($this->tableName,'role_id='.$id,array('state'=>0));
	}
}